<?php
define('PHP5_PHP', true);
global $php5DB, $php5DB_en,$php5Config, $php5Template, $php5User, $php5Session, $php5TemplatePath, $php5Msg, $php5Section, $language, $task, $define;
global $msgAlert, $redirect, $php5DB;
include("configs/extension.inc");
include("configs/config".$php5Ext);

$domain = $_SERVER["HTTP_HOST"];
$language = 'en';
/*if($domain=='japan.motul.com') {
	$language = 'jp';
}*/

if(!is_file($php5RootPath . "/includes/php5_common" . $php5Ext)){
	echo "To check the config file.";
	die();
}

require_once($php5RootPath . "/includes/php5_define" . $php5Ext);
require_once($php5RootPath . "/includes/class.inputfilter" . $php5Ext);
require_once($php5RootPath . "/includes/php5_common" . $php5Ext);
require_once($php5RootPath . "/includes/php5_sef" . $php5Ext );
require_once ($php5RootPath.'/includes/smarty.libs/Smarty.class.php');
//require_once($php5RootPath . "/includes/app" . $php5Ext);
//Begin get params
$admin     = php5GetParam($_REQUEST, "t", "");
$task     = php5GetParam($_REQUEST, "task", "");
$type     = php5GetParam($_REQUEST, "o", "main");
$module   = php5GetParam($_REQUEST, "m", "main");
$file     = php5GetParam($_REQUEST, "f", "");
$post    = php5GetParam($_REQUEST, "po", "");
$php5Msg 	= php5GetParam($_REQUEST, "msg", "");
$rootPath = $php5RootPath . "/";
if($admin) {
	$rootPath .= "insite/";
	require_once($php5RootPath . "/languages/".$language."/insite" . $php5Ext);	
} else {
	require_once($php5RootPath . "/languages/".$language."/outsite" . $php5Ext);
}
//End get params
// start init
php5Init( 0 );

$type = (isset($_GET['type']))?$_GET['type']:1;
$temp = (isset($_GET['temp']))?$_GET['temp']:0;
$curr_date = date("Y-m-d", time());
$referal_url = @$_GET['redirect'];

if ($type==1) {	
	$php5DB->setQuery("SELECT * FROM tbl_rate_detail WHERE date_added='".$curr_date."' AND type=".$type." AND IP='".VisitorIP()."' AND typeoftemp=".$temp);	
	$rs1 = $php5DB->query();
} else {
	$php5DB->setQuery("SELECT * FROM tbl_rate_detail WHERE date_added='".$curr_date."' AND type=".$type." AND 	typeoftemp=".$temp);
	$rs1 = $php5DB->query();
}
$num_rows1 = $php5DB->getNumRows($rs1);

if ($num_rows1==0){
	$php5DB->setQuery("SELECT * FROM tbl_rate_email WHERE date_added='".$curr_date."' AND type=".$type." AND 	typeoftemp=".$temp);
	$rs2 = $php5DB->query();
	$num_rows = $php5DB->getNumRows($rs2);
	if ($num_rows>0){
		$query = "UPDATE tbl_rate_email SET count = count+1 WHERE date_added='".$curr_date."' AND type=".$type." AND typeoftemp=".$temp;
		$php5DB->setQuery($query);
		$php5DB->query();
	} else {
		$query = "INSERT INTO tbl_rate_email (count, date_added, type, 	typeoftemp) VALUES (1, '".$curr_date."', ".$type.", ".$temp.")";
		$php5DB->setQuery($query);
		$php5DB->query();
		$query1 = "INSERT INTO tbl_rate_detail (IP, date_added, type, referal_url,typeoftemp) VALUES ('".VisitorIP()."', '".$curr_date."', ".$type.", '".$referal_url."', ".$temp.")";
		$php5DB->setQuery($query1);
		$php5DB->query();
	}
} 

if ($type==1){
	header('Location: '.$referal_url);
}

function VisitorIP(){ 
    if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
        $TheIp=$_SERVER['HTTP_X_FORWARDED_FOR'];
    else $TheIp=$_SERVER['REMOTE_ADDR'];
 
    return trim($TheIp);
}
echo "Done";
?>